import Vue from 'vue'
import Router from 'vue-router'
import Login from "../components/Login";


Vue.use(Router)


//解决路由重复点击报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



const routes=[
  //路由重定向，没有路由地址时默认跳转到登录页
  {
    path:'/',
    redirect:'/login'
  },
  { path: '/login',component: Login},
  {
    path: '/home',
    component: () => import('@/components/home'),
    redirect: '/welcome',
    children:[
      {path:'/welcome',component:()=>import('@/components/welcome')},
      {path:'/users',component:()=>import('@/components/user/users')},
      {path:'/rights',component:()=>import('@/components/power/rights')},
      {path:'/roles',component:()=>import('@/components/power/roles')},
      {path:'/categories',component:()=>import('@/components/goods/Cate')},
      {path:'/params',component:()=>import('@/components/goods/params')},
      {path:'/goods',component:()=>import('@/components/goods/List')},
      {path:'/goods/add',component:()=>import('@/components/goods/add')},
      {path:'/orders',component:()=>import('@/components/order/order')},
    ]
  },
]

const router = new Router({
  'mode':'history',//history模式去掉导航中的#
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to:将要跳转的路径
  //from: 代表从哪个路径跳转过来
  //next:一个函数，代表着放行
  //newt()  放行  next('/login')强制跳转
  if(to.path==='/login') return next();
  //获取token,没有就去登录 有就继续放行
  const Token = window.sessionStorage.getItem('token');
  if(!Token) return  next('/login');
  next();
})
export default router
